package com.example.controller;

import com.example.common.Result;
import com.example.entity.Booking;
import com.example.service.BookingService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/booking")
public class BookingController {

    @Resource
    private BookingService bookingService;

    @PostMapping("/add")
    public Result add(@RequestBody Booking booking) {
        // 新增：校验userId非空（与Service层双重保障）
        if (booking.getUserId() == null) {
            return Result.error("400", "用户ID不能为空");
        }
        bookingService.add(booking);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Booking booking) {
        bookingService.update(booking);
        return Result.success();
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        bookingService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        bookingService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id) {
        Booking booking = bookingService.selectById(id);
        return Result.success(booking);
    }

    @GetMapping("/selectAll")
    public Result selectAll(Booking booking) {
        List<Booking> list = bookingService.selectAll(booking);
        return Result.success(list);
    }

    @GetMapping("/selectPage")
    public Result selectPage(Booking booking,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Booking> pageInfo = bookingService.selectPage(booking, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @GetMapping("/selectByUserId/{userId}")
    public Result selectByUserId(@PathVariable Long userId) {
        List<Booking> list = bookingService.selectByUserId(userId);
        return Result.success(list);
    }

    @PostMapping("/create")
    public Result createBooking(@RequestBody Booking booking) {
        try {
            // 参数校验
            if (booking.getUserId() == null) {
                return Result.error("400", "用户ID不能为空");
            }
            if (booking.getHotelId() == null) {
                return Result.error("400", "酒店ID不能为空");
            }
            if (booking.getRoomId() == null) {
                return Result.error("400", "房间ID不能为空");
            }
            
            // 创建预订
            Booking createdBooking = bookingService.createBooking(booking);
            
            // 再次查询以获取完整的关联信息
            Booking fullBooking = bookingService.selectById(createdBooking.getId());
            
            return Result.success(fullBooking);
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }

    @GetMapping("/checkAvailability")
    public Result checkAvailability(@RequestParam Long roomId, 
                                   @RequestParam String checkInDate,
                                   @RequestParam String checkOutDate) {
        boolean available = bookingService.checkRoomAvailability(roomId, checkInDate, checkOutDate);
        return Result.success(available);
    }

    @GetMapping("/listByUserId/{userId}")
    public Result listByUserId(@PathVariable Long userId) {
        List<Booking> bookings = bookingService.selectByUserId(userId);
        return Result.success(bookings);
    }

    @PutMapping("/update/{id}")
    public Result updateBooking(@PathVariable Long id, @RequestBody Booking booking) {
        booking.setId(id);
        bookingService.update(booking);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        bookingService.deleteById(id);
        return Result.success();
    }

}